﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class lensManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            this.binddataFilmColor();
            this.binddata();            
            PanelInput.Visible = false;
        }
    }

    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        binddata();
        int count = ((DataSet)GridView1.DataSource).Tables[0].Rows.Count;
        if (count > 0)
        {
            LabelResultCount.Text = "找到 " + count + " 条符合条件的记录!";
        }
        else
        {
            LabelResultCount.Text = "对不起, 没有符合条件的记录!";
        }
    }

    private void binddataFilmColor()
    {
        DropDownListFilm.DataSource = CSUtils.getAllFilmColorList();
        DropDownListFilm.DataBind();
    }
    
    private void binddata() {
        GridView1.DataSource = this.getAllLensBase();
        GridView1.DataBind();        
    }


    private DataSet getAllLensBase() {
        string sql =
            "select * " +
            "from cs_lens_base lens {0} " +
            "order by lens_name";


        string where = "where market = '内销' ";

        string lensName = TextBoxLensName_Q.Text;
        if (lensName.Trim() != "")
        {
            where += "and lens_name like '%" + lensName + "%' ";
        }

        string refractive = DropDownListrRefractive_Q.SelectedValue;
        if (refractive.Trim() != "-1")
        {
            where += "and refractive_index = '" + refractive + "' ";
        }

        sql = string.Format(sql, where);

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {        

        if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add")) {
            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;
            LabelLensID.Text = e.CommandArgument.ToString();
            LensVO vo = CSUtils.getLens(LabelLensID.Text);
            TextBoxLensName.Text = vo.Name;
            DropDownListLensType.SelectedValue = vo.Type;
            DropDownListrRefractive.SelectedValue = vo.RefractiveIndex;
            DropDownListFilm.SelectedValue = vo.FilmColor;
            TextBoxDispersive.Text = vo.DispersiveCoef;
        }
        else if (e.CommandName.Equals("command_visible"))
        {

            string id = e.CommandArgument.ToString();

            if (this.updateLensVisible(id))
            {
                //JScript.Alert("保存成功！");
            }
            else
            {
                JScript.Alert("保存失败！");
            }

            this.binddata();
        }
        else if (e.CommandName.Equals("command_del"))
        {
            string id = e.CommandArgument.ToString();

            if (this.doDelete(id))
            {
            }
            else
            {
                JScript.Alert("删除失败, 此数据已被其他功能引用!\\n请先删除相关数据!");
            }

            this.binddata();
        }
        else {
            this.cleanPanelInput();
        }
        
        
    }

    private bool doDelete(string id)
    {

        string sqlDel =
            "delete cs_lens_base " +
            "where lens_id = " + id;

        DBConn myDB = new DBConn();
        int tmp;
        try
        {
            tmp = myDB.Delete(sqlDel);
        }
        catch
        {
            myDB.Close();
            return false;
        }
        myDB.Close();
        return (tmp > 0);

    }

    protected void ButtonSubmit_Click(object sender, EventArgs e) {
        
        string lensID = LabelLensID.Text;
        string lensName = TextBoxLensName.Text;
        string lensType = DropDownListLensType.SelectedValue;
        string refractiveIndex = DropDownListrRefractive.SelectedValue;
        string filmColor = DropDownListFilm.SelectedValue;
        string dispersiveCoef = TextBoxDispersive.Text;

        string commandType = TextBoxCommandType.Text;
        LensVO vo = null;
        if (commandType.Equals("command_edit"))
        {
            //get vo from db
            vo = CSUtils.getLens(lensID);

            //update there info
            vo.Name = lensName;
            vo.Type = lensType;
            vo.RefractiveIndex = refractiveIndex;
            vo.FilmColor = filmColor;
            vo.DispersiveCoef = dispersiveCoef;
        }
        else if (commandType.Equals("command_add"))
        {
            if (CSUtils.haveLens(lensName, "内销"))
            {
                JScript.Alert("该品名已经存在, 请不要重复添加!");
                return;
            }
            vo = new LensVO();
            vo.Name = lensName;         
            vo.Type = lensType;
            vo.RefractiveIndex = refractiveIndex;
            vo.FilmColor = filmColor;
            vo.DispersiveCoef = dispersiveCoef;
            vo.Market = "内销";
        }

        if (CSUtils.mergeLens(vo))
        {
            //JScript.Alert("保存成功！");
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();
        
    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e) {
        PanelInput.Visible = true;
        TextBoxCommandType.Text = "command_add";
        this.cleanPanelInput();
    }



    private void cleanPanelInput() {
        LabelLensID.Text = "";
        TextBoxLensName.Text = "";
        DropDownListLensType.SelectedIndex = 0;
        DropDownListrRefractive.SelectedIndex = 0;
        DropDownListFilm.SelectedIndex = 0;
        TextBoxDispersive.Text = "";


    }

    private bool updateLensVisible(string lens_id)
    {

        string sqlUpdate =
            "update cs_lens_base " +
            "set visible = visible-1 " +
            "where lens_id = " + lens_id;

        DBConn myDB = new DBConn();
        int tmp = myDB.Update(sqlUpdate);
        myDB.Close();
        return (tmp == 1);

    }
}
